package utils.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.myapplication.R;

import java.util.List;

import entity.ListClassicEntitiy;
import tools.ItemOnClickListener;
import volley.VolleySingleton;

/**
 * 经典页面适配器
 * Created by dllo on 15/11/24.
 */
public class ListClassicAdpter extends RecyclerView.Adapter<ListClassicAdpter.RecyerAdpter> {
    private List<ListClassicEntitiy.ListEntity> listEntities;
    private ItemOnClickListener itemOnClickListener;
    public void setItem(ItemOnClickListener itemOnClickListener){
        this.itemOnClickListener = itemOnClickListener;
    }

    public void addData(List<ListClassicEntitiy.ListEntity> listEntities) {
        this.listEntities = listEntities;
        notifyDataSetChanged();
    }


    //加载布局
    @Override
    public RecyerAdpter onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.list_classic_item, null);
        return new RecyerAdpter(view);
    }

    //得到对应的数据
    @Override
    public void onBindViewHolder(RecyerAdpter holder, int position) {
        if (listEntities != null) {
            ListClassicEntitiy.ListEntity list = listEntities.get(position);
            VolleySingleton.getInstance().getBitmap(list.getCoverMiddle(), holder.classicImg);


            holder.classicpositonTv.setText(position + 1 + "");
            if (position == 0)
                holder.classicpositonTv.setTextColor(0xFFF14E2F);
            if (position == 1)
                holder.classicpositonTv.setTextColor(0xFFF28600);
            if (position == 2)
                holder.classicpositonTv.setTextColor(0xFF8FB000);
            if (position != 0 && position != 1 && position != 2)
                holder.classicpositonTv.setTextColor(0xFFcdcdcd);
            holder.ClassicTitle.setText(list.getTitle());
            holder.classicContent.setText(list.getTracksCounts() + "集");
            //   holder.classicContent.setText(list.getTags());
            holder.tags.setText(list.getTags());

            holder.pos = position;
        }


    }

    //返回的数据长度
    @Override
    public int getItemCount() {
        return listEntities != null && listEntities.size() > 0 ? listEntities.size() : 0;
    }

    //初始化数据
    public class RecyerAdpter extends RecyclerView.ViewHolder implements View.OnClickListener {
        private NetworkImageView classicImg;
        private ImageView gotonext;
        private RelativeLayout item;
        private TextView classicpositonTv, ClassicTitle, classicContent, classicTime, tags;

        private  int pos;
        public RecyerAdpter(View itemView) {
            super(itemView);
            classicImg = (NetworkImageView) itemView.findViewById(R.id.classicImg);
            gotonext = (ImageView) itemView.findViewById(R.id.gotonext);
            classicContent = (TextView) itemView.findViewById(R.id.classicContent);
            classicpositonTv = (TextView) itemView.findViewById(R.id.classicpositonTv);
            ClassicTitle = (TextView) itemView.findViewById(R.id.ClassicTitle);
            tags = (TextView) itemView.findViewById(R.id.tags);
            item = (RelativeLayout) itemView.findViewById(R.id.item);
            item.setOnClickListener(this);
            //  classicTime = (TextView) itemView.findViewById(R.id.classicTime);

        }

        @Override
        public void onClick(View v) {

            switch (v.getId()){
                case R.id.item:
                    if (listEntities != null && listEntities.size()>0){
                        ListClassicEntitiy.ListEntity list = listEntities.get(pos);
                        if (itemOnClickListener!= null){
                            itemOnClickListener.itemListener(list);
                        }
                    }

                    break;
            }
        }
    }
}
